﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBlog.Model;
using MyBlog.DataAccess;
using System.Linq.Expressions;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessAlbumDetail : IBusinessAlbumDetail
    {
        IDALAlbumDetail _dalAlbumDetail;
        IUnitOfWork _unitWork;
        public BusinessAlbumDetail(IDALAlbumDetail dalAlbumDetail, IUnitOfWork unitWork)
        {
            _dalAlbumDetail = dalAlbumDetail;
            _unitWork = unitWork;
        }
        public void Add(AlbumDetailModel model)
        {
            var dataEntity = Mapper.Map<Tbl_AlbumDetail>(model);
            _dalAlbumDetail.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(AlbumDetailModel model)
        {
            var dataEntity = Mapper.Map<Tbl_AlbumDetail>(model);
            _dalAlbumDetail.Update(dataEntity);
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<AlbumDetailModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_AlbumDetail>>(models);
            _dalAlbumDetail.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<AlbumDetailModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_AlbumDetail>>(models);
            _dalAlbumDetail.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<AlbumDetailModel> GetAll(bool isTracking = true)
        {
            var entities = _dalAlbumDetail.GetAll(isTracking);
            return Mapper.Map<List<AlbumDetailModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalAlbumDetail.Delete(primaryKey);
            _unitWork.Commit();
        }

        public AlbumDetailModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalAlbumDetail.Get(primaryKey);
            return Mapper.Map<AlbumDetailModel>(dataEntity);
        }

        public void Delete(AlbumDetailModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_AlbumDetail>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalAlbumDetail.Delete(where);
            _unitWork.Commit();
        }

        public AlbumDetailModel Get(AlbumDetailModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_AlbumDetail>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (model.GroupID != 0)
            {
                where = where.And(x => x.GroupID == model.GroupID);
            }
            var dataEntity = _dalAlbumDetail.Get(where);
            return Mapper.Map<AlbumDetailModel>(dataEntity);
        }

        public List<AlbumDetailModel> GetAll(AlbumDetailModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_AlbumDetail>();
            if (model.GroupID != 0)
            {
                where = where.And(x => x.GroupID == model.GroupID);
            }
            var entities = _dalAlbumDetail.GetAll(where, isTracking);
            return Mapper.Map<List<AlbumDetailModel>>(entities);
        }


        public List<AlbumDetailModel> GetAll(AlbumDetailModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }
    }
}
